#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

// 接收温湿度数据的线程函数
typedef struct {
    char username[1024];
    char password[1024];
} User;

User users[100]; // 存储最多100个用户
int numUsers = 0; // 当前的用户数量

void register_user(const char* username, const char* password) {
    strncpy(users[numUsers].username, username, sizeof(users[numUsers].username) - 1);
    strncpy(users[numUsers].password, password, sizeof(users[numUsers].password) - 1);
    numUsers++;
}

int check_login(const char* username, const char* password) {
    for(int i = 0; i < numUsers; i++) {
        if(strcmp(users[i].username, username) == 0 && strcmp(users[i].password, password) == 0) {
            return 1; // 登录成功
        }
    }
    return 0; // 登录失败
}

void* receive_temperature_data(void* arg) {
    int conn_fd = *(int*)arg;

    // char buffer[1024];
    // ssize_t numBytesRcvd;
    // while((numBytesRcvd = recv(conn_fd, buffer, sizeof(buffer) - 1, 0)) > 0) {
    //     buffer[numBytesRcvd] = '\0';
    //     printf("Received temperature data: %s\n", buffer);
    // }

    // if(numBytesRcvd < 0) {
    //     perror("recv failed");
    // }
    long long  msg[3];

    FILE* fp=fopen("1.txt","w");
	if (fp == NULL) {
        printf("无法打开文件。\n");
    	break; 
	}

while (1)
{

    memset(msg, 0, sizeof(msg)); // 初始化数组，数组中的值全部赋值为0

    ssize_t numBytesRcvd = recv(conn_fd, msg, sizeof(msg), 0);
    if(numBytesRcvd < 0) {
        perror("recv failed");
        break;
    } else if(numBytesRcvd == 0) {
        printf("Client disconnected\n");
        break;
    }

    if(sizeof(msg) > 0) {
        printf("Received: %lld %f %f\n", msg[0],(float)msg[1],(float)msg[2]);
        fprintf(fp, "%lld %f %f\n", msg[0],(float)msg[1],(float)msg[2]);
    } else {
        printf("Received an empty message\n");
    }
}




    return NULL;
}

// 处理用户注册和登录的线程函数
void* handle_user_login(void* arg) {
    int conn_fd = *(int*)arg;

    while(1) {
        char data[200];
        ssize_t numBytesRcvd;
        if((numBytesRcvd = recv(conn_fd, data, sizeof(data) - 1, 0)) > 0) {
            data[numBytesRcvd] = '\0';
            printf("Received data: %s\n", data);

            char type[10];
            char username[1024];
            char password[1024];
            sscanf(data, "%s %s password: %s", type, username, password);

            if (strncmp(type, "register:", 9) == 0) {
                register_user(username, password);
                printf("Registered new user: %s\n", username);
            } else if (strncmp(type, "login:", 6) == 0) {
                if(check_login(username, password)) {
                    printf("Login successful\n");
                    // 创建接收温湿度数据的线程
                    pthread_t thread;
                    pthread_create(&thread, NULL, receive_temperature_data, &conn_fd);
                    break;
                } else {
                    printf("Login failed\n");
                }
            }
        }

        if(numBytesRcvd < 0) {
            perror("recv failed");
        }
    }

    return NULL;
}

int main(int argc, char *argv[]) {
    // 创建套接字并绑定地址
    int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
    if(socket_fd < 0) {
        perror("socket creation failed");
        return -1;
    }

    struct sockaddr_in srvaddr;
    srvaddr.sin_family = AF_INET;
    srvaddr.sin_port = htons(55555);
    srvaddr.sin_addr.s_addr = inet_addr("192.168.64.7");

    if(bind(socket_fd, (struct sockaddr *)&srvaddr, sizeof(srvaddr)) < 0) {
        perror("bind failed");
        return -1;
    }

    // 监听
    if(listen(socket_fd, 4) < 0) {
        perror("listen failed");
        return -1;
    }

    printf("Waiting for client connection...\n");

    // 等待客户连接
    int conn_fd = accept(socket_fd, NULL, NULL);
    if(conn_fd < 0) {
        perror("accept failed");
        return -1;
    }

    printf("连接成功\n");

    // 创建线程
    pthread_t thread;
    pthread_create(&thread, NULL, handle_user_login, &conn_fd);

    // 等待线程结束
    pthread_join(thread, NULL);

    // 关闭套接字
    close(socket_fd);
    close(conn_fd);

    return 0;
}